package com.wanqh.reptitle.utils;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.google.common.collect.Lists;
import com.wanqh.reptitle.pojo.ProductExcel;
import com.wanqh.reptitle.pojo.SearchResult;
import io.appium.java_client.android.AndroidDriver;
import lombok.NonNull;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.util.List;

public class SpiderUtils {


    public static final String FILE_PATH = "C:\\Users\\Administrator\\Desktop\\wanqh-reptitle\\src\\test\\resources\\搜索结果_new.xlsx";

    public static List<ProductExcel> getData() {
        return EasyExcel.read("C:\\Users\\Administrator\\Desktop\\wanqh-reptitle" +
                        "\\src\\test\\resources\\20201218版本.xlsx",
                ProductExcel.class, null)
                .ignoreEmptyRow(true)
                .sheet("吴珊珊")
                .headRowNumber(1)
                .doReadSync();
    }

    public static List<SearchResult> getResultData(String sheetName) {
        return EasyExcel.read(FILE_PATH, SearchResult.class, null)
                .ignoreEmptyRow(true)
                .sheet(sheetName)
                .doReadSync();
    }

    public static void excuteAdbShell(String s) {
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec(s);
        } catch (Exception e) {
            System.out.println("执行命令:" + s + "出错");
        }
    }


    /**
     * 从剪贴板中获取文本（粘贴）
     */
    public static String getClipboardString() {
        // 获取系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

        // 获取剪贴板中的内容
        Transferable trans = clipboard.getContents(null);

        if (trans != null) {
            // 判断剪贴板中的内容是否支持文本
            if (trans.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                try {
                    // 获取剪贴板中的文本内容
                    String text = (String) trans.getTransferData(DataFlavor.stringFlavor);
                    return text;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    /**
     * 判断搜索结果是否包含在关键字中
     * @param result
     * @param keyword
     * @return
     */
    public static boolean isContains(String result,@NonNull String keyword) {
        if(StrUtil.isEmpty(result)) {
            return false;
        }
        String[] split = keyword.split("/");
        for (String s : split) {
            if(result.toLowerCase().contains(s.toLowerCase())) {
                return true;
            }
        }
        return false;
    }

    public static boolean elementIsExit(AndroidDriver driver, By by) {
        try {
            WebElement element = driver.findElement(by);
            if (element != null) {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }


    public static void writeResult(Class clazz, String sheetName, @NonNull Object data) {
        ExcelReaderBuilder read = EasyExcel.read(FILE_PATH, clazz, null);
        List<Object> exitData = read.sheet(sheetName).doReadSync();
        if (exitData == null) {
            exitData = Lists.newArrayList();
        }
        exitData.add(data);
        EasyExcel.write(FILE_PATH, clazz).sheet(sheetName).doWrite(exitData);
    }


    public static String getSearchBrand(String brand) {
        String searchBrand = brand;
        if (brand.contains("/")) {
            String[] split = brand.split("/");
            searchBrand = split.length >= 2 ? split[1] : searchBrand;
        }
        return searchBrand;
    }
}
